package com.example.bean;

import com.example.entity.ReleaseNote;
import com.example.util.TimeUtil;

public class ReleaseNoteBean implements Comparable<ReleaseNoteBean> {
    private Long objId;
    private String version;
    private String content;
    private String publishdate;
    public static String strFormat = "yyyy-MM-dd HH:mm:ss";

    public void fromDto(ReleaseNote releasenote) {
        this.objId = releasenote.getObjId();
        this.version = releasenote.getVersion();
        this.content = releasenote.getContent();
        this.publishdate = TimeUtil.timestampToString(releasenote.getPublishDate());
    }

    public ReleaseNote toDto() {
        ReleaseNote releaseNote = new ReleaseNote(); // ? is this one not controlled by bean factory
        try {
            releaseNote.setObjId(this.objId);
        } catch (NullPointerException e) {
            System.out.println("NullPointerException: ReleaseNoteBean.objId");
        }
        releaseNote.setVersion(this.version);
        releaseNote.setContent(this.content);
        releaseNote.setPublishDate(TimeUtil.stringToTimestamp(this.publishdate));
        return releaseNote;
    }

    public Long getObjId() {
        return objId;
    }

    public void setObjId(Long objId) {
        this.objId = objId;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getPublishdate() {
        return publishdate;
    }

    public void setPublishdate(String publishdate) {
        this.publishdate = publishdate;
    }

    @Override
    public int compareTo(ReleaseNoteBean bean) {
        int compareResult = 0;
        String[] versionArr = this.version.split("\\.");
        String[] otherVersionArr = bean.getVersion().split("\\.");
        if (versionArr.length > otherVersionArr.length) {
            String[] newOtherVersionArr = new String[versionArr.length];
            for (int i = 0; i < otherVersionArr.length; i++) {
                newOtherVersionArr[i] = otherVersionArr[i];
            }
            for (int i = otherVersionArr.length; i < versionArr.length; i++) {
                newOtherVersionArr[i] = "0";
            }
            otherVersionArr = newOtherVersionArr;
        }
        if (versionArr.length < otherVersionArr.length) {
            String[] newVersionArr = new String[otherVersionArr.length];
            for (int i = 0; i < versionArr.length; i++) {
                newVersionArr[i] = versionArr[i];
            }
            for (int i = versionArr.length; i < otherVersionArr.length; i++) {
                newVersionArr[i] = "0";
            }
            versionArr = newVersionArr;
        }
        for (int i = 0; i < versionArr.length; i++) {
            if (Integer.valueOf(versionArr[i]) < Integer.valueOf(otherVersionArr[i])) {
                compareResult = -1;
                break;
            }
            if (Integer.valueOf(versionArr[i]) > Integer.valueOf(otherVersionArr[i])) {
                compareResult = 1;
                break;
            }
        }
        return compareResult;
    }
}
